package com.zuorenke.hessian;

import com.caucho.hessian.client.HessianProxyFactory;
import com.zuorenke.hessian.client.IFileTransRemoteService;
import com.zuorenke.hessian.client.IKlassRemoteService;
import com.zuorenke.hessian.client.ILessonRemoteService;
import com.zuorenke.hessian.client.IQuestionRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.MalformedURLException;

/**
 * @author Z.R.K
 * @description
 * @create 2019-05-27 19:25:42
 **/
@Configuration
@Slf4j
public class HessianProxyConfig implements InitializingBean, ApplicationContextAware {
	private ApplicationContext applicationContext;
	
	private HessianProxyFactory factory;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		factory = new HessianProxyFactory();
		factory.setConnectTimeout(3000L);
		factory.setReadTimeout(10000L);
	}
	
	@Bean
	public ILessonRemoteService lessonRemoteService() {
		try {
			return (ILessonRemoteService) factory.create(ILessonRemoteService.class
					,"https://lesson.leke.cn/api/n/lesson/lessonRemoteService.ws");
		} catch (MalformedURLException e) {
			return null;
		}
	}
	
	
	@Bean
	public IKlassRemoteService klassRemoteService() {
		try {
			return (IKlassRemoteService) factory.create(IKlassRemoteService.class
					,"https://lesson.leke.cn/api/n/lesson/klassRemoteService.ws");
		} catch (MalformedURLException e) {
			return null;
		}
	}
	
	@Bean
	public IFileTransRemoteService fileTransRemoteService() {
		try {
			return (IFileTransRemoteService) factory.create(IFileTransRemoteService.class
					,"https://fs.leke.cn/api/n/fs/fileTransRemoteService.ws");
		} catch (MalformedURLException e) {
			return null;
		}
	}
	
	@Bean
	public IQuestionRemoteService questionRemoteService() {
		try {
			return (IQuestionRemoteService) factory.create(IQuestionRemoteService.class
					,"https://question.leke.cn/api/n/question/questionRemoteService.ws");
		} catch (MalformedURLException e) {
			return null;
		}
	}
	
}
